﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebCrawler.HttpConnection;

namespace WebCrawler.HtmlParser
{
    public class BruteForceParser
    {
        private string _serverUrl;
        private string _pathUrl;
        private string _content;

        public BruteForceParser(string serverUrl, string pathUrl, string content)
        {
            _serverUrl = serverUrl;
            _pathUrl = pathUrl;
            _content = content;
        }

        public IList<string> GetAllLinks()
        {
            //HttpRequest request = new HttpRequest("http://public.ucn.dk/fen/testsite/Index.htm");
            List<string> links = new List<string>();
            int i, j, k;
            string pattern = "href";
            //_content = request.Content;
            string link = "";
            string match = "";

            for (j = 0; j < _content.Length; j++)
            {
                match = "";
                for (i = 0; i < pattern.Length && pattern[i].Equals(_content[j]); ++i)
                {
                    //Console.Write(pattern[i]);
                    j++;
                    match += pattern[i];
                    if (match.Equals(pattern))
                    {
                        for (k = j + 2; k < _content.Length && !_content[k].Equals('\u0022'); ++k) //k = j + 2 for at springe '=' og '"' over efter href
                        {
                            link += _content[k];
                            //Console.Write(content[k]);
                        }
                        links.Add(link);
                        link = "";
                    }
                }
            }
            foreach (string l in links)
            {
                Console.WriteLine(l);
            }
            return links;
        }

        //Test
        public void TestGetAllLinks()
        {
            HttpRequest request = new HttpRequest("http://public.ucn.dk/fen/testsite/Index.htm");
            List<string> links = new List<string>();
            int i, j, k;
            string pattern = "href";
            string content = request.Content;
            string link = "";
            string match = "";

            for (j = 0; j < content.Length; j++)
            {
                match = "";
                for (i = 0; i < pattern.Length && pattern[i].Equals(content[j]); ++i)
                {
                    //Console.Write(pattern[i]);
                    j++;
                    match += pattern[i];
                    if (match.Equals(pattern))
                    {
                        for (k = j + 2; k < content.Length && !content[k].Equals('\u0022'); ++k)
                            //k = j + 2 for at springe '=' og '"'
                        {
                            link += content[k];
                            //Console.Write(content[k]);
                        }
                        links.Add(link);
                        link = "";
                    }
                }
            }
            foreach (string l in links)
            {
                Console.WriteLine(l);
            }
            Console.ReadKey();
            //return links;
        }
    }
}
